package com.lzt.admin.common.security.handler;

import com.lzt.admin.common.utils.PrintResponseUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;
@Component
@RequiredArgsConstructor
public class LztAuthenticationFailureHandler implements AuthenticationFailureHandler {
    private final PrintResponseUtil printResponseUtil;
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        String msg = exception.getMessage().indexOf("Maximum sessions of") >= 0 ? "该用户已在其它设备上登录，请联系系统管理员" : exception.getMessage();
        printResponseUtil.printFail(request,response,msg);
    }
}
